<template>
  <PanleHead :route="route"></PanleHead>
  <div class="btns">
    <el-button @click="open(null)" type="primary" icon="Plus" size="small"
      >新增</el-button
    >
    <el-popconfirm
      width="220"
      confirm-button-text="是"
      cancel-button-text="否"
      icon="InfoFilled"
      icon-color="#626AEF"
      title="是否确认删除?"
      @confirm="comfirmEvent"
    >
      <template #reference>
        <el-button type="danger" icon="Delete" size="small">删除</el-button>
      </template>
    </el-popconfirm>
  </div>
  <el-table
    :data="tableData.list"
    style="width: 100%"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="name" label="昵称"></el-table-column>
    <el-table-column label="头像">
      <template #default="scope">
        <el-image
          style="width: 50px; height: 50px"
          :src="scope.row.avatar"
        ></el-image>
      </template>
    </el-table-column>
    <el-table-column prop="sex" label="性别">
      <template #default="scope">
        {{ scope.row.sex === 1 ? "男" : "女" }}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号"> </el-table-column>
    <el-table-column prop="active" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.active ? 'success' : 'danger'">{{
          scope.row.active ? "正常" : "失效"
        }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="创建时间">
      <template #default="scope">
        <div class="felx-box">
          <el-icon><Clock /></el-icon>
          <span style="padding-left: 10px">{{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <div>
          <el-button type="primary" @click="open(scope.row)">编辑</el-button>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination
      v-model:current-page="paginationData.pageNum"
      :page-size="paginationData.pageSize"
      :background="false"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      size="small"
    />
  </div>
  <el-dialog
    v-model="dialogFromVisiable"
    :before-close="beforeClose"
    title="陪护师添加"
    width="500"
  >
    <el-form
      ref="formRef"
      label-width="100px"
      label-position="left"
      :model="form"
      :rules="rules"
    >
      <el-form-item v-show="false" prop="id">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-button
          type="primary"
          v-if="!form.avatar"
          @click="dilagImgvisiable = true"
          >点击上传</el-button
        >
        <el-image
          v-else
          :src="form.avatar"
          style="width: 100px; height: 100px"
        ></el-image>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input-number
          v-model="form.age"
          :min="18"
          :max="50"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="是否生效" prop="active">
        <el-radio-group v-model="form.active">
          <el-radio :value="0">失效</el-radio>
          <el-radio :value="1">生效</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog
    v-model="dilagImgvisiable"
    :before-close="beforeClose"
    title="选择图片"
    width="680"
  >
    <div class="image-list">
      <div
        v-for="(item, index) in fileList"
        :key="item.name"
        class="img-box"
        @click="selectindex = index"
      >
        <div class="select" v-if="selectindex === index">
          <el-icon color="#fff"><Check /></el-icon>
        </div>
        <el-image style="width: 100px; height: 100px" :src="item.url">
        </el-image>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="dilagImgvisiable = false"
          >取消</el-button
        >
        <el-button type="primary" @click="confirmImge()">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {
  Createpehushi,
  DeletePEihushi,
  getPeihushiList,
  PhotoList,
} from "@/api";
import PanleHead from "@/components/PanleHead.vue";
import { ElMessage } from "element-plus";
import { nextTick, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
const route = useRoute();
const dialogFromVisiable = ref(false);
const beforeClose = () => {
  dialogFromVisiable.value = false;
  dilagImgvisiable.value = false;
  formRef.value.resetFields();
};
const formRef = ref();
const form = ref({
  id: "",
  mobile: "",
  active: 1,
  age: "",
  avatar: "",
  name: "",
  sex: "",
});
const rules = ref({
  name: [
    {
      required: true,
      trigger: "blur",
      message: "请输入权限名称",
    },
  ],
  avatar: [
    {
      required: true,
      message: "请选择头像",
    },
  ],
  sex: [
    {
      required: true,
      trigger: "change",
      message: "请选择性别",
    },
  ],
  mobile: [
    {
      required: true,
      trigger: "blur",
      message: "请填写手机号",
    },
  ],
});
const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      console.log(form.value);

      Createpehushi(form.value).then(({ data }) => {
        if (data.code === 10000) {
          ElMessage.success("创建成功");
          beforeClose();
          getLisData();
        } else {
          ElMessage.error(data.message);
        }
      });
    } else {
      console.log("erro submit", fields);
    }
  });
};
const fileList = ref([]);
const open = (rowData) => {
  dialogFromVisiable.value = true;
  nextTick(() => {
    if (rowData) {
      Object.assign(form.value, rowData);
    }
  });
};
const dilagImgvisiable = ref(false);
const getPhtoList = async () => {
  const res = await PhotoList();
  console.log(res);

  fileList.value = res.data.data;
  console.log(fileList.value);
};
const confirmImge = () => {
  form.value.avatar = fileList.value[selectindex.value].url;
  dilagImgvisiable.value = false;
};
const paginationData = ref({
  pageNum: 1,
  pageSize: 10,
});
// 列表数据
const tableData = ref({
  list: [],
  total: 0,
});
const selectindex = ref(0);
const getLisData = async () => {
  const res = await getPeihushiList(paginationData.value);
  const { list, total } = res.data.data;
  tableData.value.list = list;
  tableData.value.total = total;
};
const handleSizeChange = (value) => {
  paginationData.value.pageSize = value;
  getLisData();
};
const handleCurrentChange = (value) => {
  paginationData.value.pageNum = value;
  getLisData();
};
const selectTabData = ref([]);
const handleSelectionChange = (value) => {
  console.log(value);
  selectTabData.value = value.map((item) => {
    return {id:item.id}
  });

  console.log(selectTabData.value);
};
// 气泡确认框
const comfirmEvent = () => {
  // 没选择的期况下
  if (!selectTabData.value.length) {
    return ElMessage.warning("请至少选择一项数据");
  } else {
    DeletePEihushi({ id: selectTabData.value }).then(({ data }) => {
      if (data.code === 10000) {
        getLisData();
      }
    });
  }
};
onMounted(() => {
  getPhtoList();
  getLisData();
});
</script>

<style scoped lang="less">
.btns {
  padding: 10px 0 10px 10px;
  background-color: #fff;
}
.image-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .img-box {
    position: relative;
    .select {
      position: absolute;
      left: 0px;
      top: 0px;
      width: 24px;
      height: 24px;
      background-color: #67c23a;
      z-index: 999;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .el-image {
    margin-right: 10px;
    margin-bottom: 10px;
  }
}
</style>
